package com.fatmeraid.controller.uploadImage;

import com.fatmeraid.common.result.Result;
import com.fatmeraid.common.utils.MinioUtil;
import com.fatmeraid.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Tag(name = "文件管理")
@RequestMapping("/file")
@RestController
public class FileUploadController {

    @Autowired
    private FileService fileService;
    @Autowired
    private MinioUtil minioUtil;

    @Operation(summary = "上传文件")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam MultipartFile file) {
        String url = fileService.upload(file);
        return Result.ok(url);
    }

    @Operation(summary = "删除文件")
    @DeleteMapping("delete")
    public Result<String> delete(String url) {
        String s = minioUtil.extractFileNameFromUrl(url);
        minioUtil.deleteFile(s);
        return Result.ok();
    }

}
